﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartConference.SmartPlayer;

namespace ScreenSharing
{
    /// <summary>
    /// PublishWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PublishWindow : Window
    {
        private InitWindow parentWin { get; set; }
        private bool IsPublish { get; set; }

        private NotifyIcon _notifyIconManager { get; set; }


        private PublishManager _publishManager { get; }

        private string RoomCode { get; }

        private string PublishUrl=> $"{ConfigurationManager.AppSettings["RtmpServer"]}/{RoomCode}";


        public PublishWindow(string roomCode,InitWindow parent)
        {
            RoomCode = roomCode;
            parentWin = parent;
            InitializeComponent();
            _publishManager = new PublishManager();
            _publishManager.RtmpConnectIsOkEvent += _publishManager_RtmpConnectIsOkEvent;
            this.Visibility = Visibility.Hidden;
            this.Loaded += PublishWindow_Loaded;
        }

        private void _publishManager_RtmpConnectIsOkEvent(bool isOk,string info)
        {
            IsPublish = isOk;
           //if(!isOk)
          // _notifyIconManager.ShowBalloonTip(500,"提示",info, isOk? ToolTipIcon.None:ToolTipIcon.Error);
            _notifyIconManager.Text = isOk ? "正在推送同屏数据..." : info;
        }

        private void PublishWindow_Loaded(object sender, RoutedEventArgs e)
        {
            parentWin?.Close();
            DoPublish();
        }

        private void DoPublish()
        {
            if (_publishManager == null || !_publishManager.IsSdkInit) return;
            _publishManager.Start(PublishUrl);
            if (!_publishManager.IsPublishing) return;
            IsPublish = true;
            NotificationType();
        }

        public  void NotificationType()
        {
            _notifyIconManager = new NotifyIcon
            {
                BalloonTipText = "开始推送同屏数据",
                Text = $@"正在推送同屏数据...",
                Visible = true,
                Icon = ScreenSharing.Properties.Resources.play_green
            };
            var menu = new System.Windows.Forms.ContextMenu();
            var closeItem = new System.Windows.Forms.MenuItem
            {
                Text = "退出"
            };
            closeItem.Click += StopPublish;
          

            var stopItem = new System.Windows.Forms.MenuItem
            {
                Text = "暂停/继续"
            };
            stopItem.Click += Pause;

            menu.MenuItems.Add(stopItem);
            menu.MenuItems.Add(closeItem);

            _notifyIconManager.ContextMenu = menu;    //设置NotifyIcon的右键弹出菜单
            //设置程序启动时显示的文本
            //最小化到托盘时，鼠标点击时显示的文本
            //程序图标
            //_notifyIconManager.ShowBalloonTip(3000);

            var timer1=new System.Timers.Timer();
            timer1.Elapsed += Timer1_Elapsed;
            timer1.Enabled = true;
            timer1.Interval = 500;
            timer1.Start();
           
        }

        private void Pause(object sender, EventArgs e)
        {
            if (_publishManager.IsPublishing)
            {
                _publishManager?.End();
            }
            else
            {
                _publishManager?.Start(PublishUrl);
            }
                
        }

        public bool IsPublishing { get; set; }
        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!_publishManager.IsPublishing)
            {
                _notifyIconManager.Icon = ScreenSharing.Properties.Resources.play_gray;
                return;
            }
            _notifyIconManager.Icon = IsPublishing ? ScreenSharing.Properties.Resources.play_gray : ScreenSharing.Properties.Resources.play_green;
            IsPublishing = !IsPublishing;
        }

        private  void StopPublish(object sender, EventArgs e)
        {
            StopPublish();
        }


        private void StopPublish()
        {
            _publishManager?.End();

            this.Close();
        }
    }
}
